// pages/index/invited/index.js
const app = getApp();
import Toast from "../../../vant/weapp/toast/toast";
Page({
  data: {
    headTop: "",
    headHeight: "",
    sumHeight: "",
    detail: [],
    latitude: "",
    longitude: "",
    is_user_show: true,
    is_user: 0,
    showUser: false,
  },
  onLoad(options) {
    let that = this;
    that.getShareOrder(options.order_on)
    that.setData({
      headTop: app.globalData.headTop,
      headHeight: app.globalData.headHeight,
      sumHeight: app.globalData.sumHeight,
    });
  },
  onShow() {
    const that = this
    wx.getLocation({
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        });
        wx.setStorageSync("openSet", false);
      },
      fail: function (fail) {},
    });
  },
  //   登录
  login() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    } else if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 2
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)
    wx.setStorageSync('mobile', e.detail.mobile)
  },
  // 打开设置
  openSet() {
    var that = this;
    if (wx.getStorageSync("openSet")) {
      wx.getSetting({
        success: (res) => {
          var statu = res.authSetting;
          if (!statu["scope.userLocation"]) {
            //没授权
            wx.showModal({
              title: '授权',
              content: '如果需要使用定位功能，请点击确定并在授权管理中打开“设置”，打开定位功能。',
              complete: (res) => {
                if (res.confirm) {
                  wx.openSetting({
                    success: function (res) {
                      if (res.authSetting["scope.userLocation"]) {
                        wx.setStorageSync("openSet", false);
                      } // 如果成功打开授权
                      else {
                        wx.setStorageSync("openSet", true);
                        that.setData({
                          latitude: 0,
                          longitude: 0,
                        });
                      } // 如果用户依然拒绝授权
                    },
                    fail: function () {
                      //调用失败，授权登录不成功
                      return;
                    },
                  });
                }

                if (res.cancel) {
                  that.setData({
                    latitude: 0,
                    longitude: 0,
                  });
                }
              }
            })

          }
        },
      });
    }
  },
  // 导航
  duo_map(e) {
    const that = this;
    let data = that.data.detail;
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        wx.openLocation({
          //​使用微信内置地图查看位置。
          latitude: Number(data.lat), //要去的纬度-地址
          longitude: Number(data.lng), //要去的经度-地址
          name: data.name,
          address: data.address,
          scale: 18,
        });
      },
      fail(e) {
        console.log(e);
        wx.setStorageSync("openSet", true);
        that.openSet();
      },
    });
  },
  // 开门
  opendoor(e) {
    console.log(e.currentTarget.dataset.index)
    const that = this
    let i = e.currentTarget.dataset.index
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        app.http.post(app.api.DoorIndex, {
          order_on: that.data.detail.order_on,
          door_id: that.data.detail.door_list[i].door_id,
          lat: res.latitude,
          lng: res.longitude
        }, res => {
          console.log(res)
          if (res.code === 401) {
            that.login()
            return
          }
          if (res.code === 200) {
            Toast.success("开门成功")
          } else {
            Toast.fail(res.message)
          }
        })
      },
      fail(e) {
        wx.setStorageSync("openSet", true);
        that.openSet();
      },
    });

  },
  getShareOrder(order_on) {
    app.http.post(app.api.ShareOrder, {
      order_on
    }, (res) => {
      console.log(res)
      if (res.code == 200) {
        this.setData({
          detail: res.data
        })
      } else {
        Toast({
          type: 'fail',
          forbidClick: true,
          message: res.message,
          onClose: () => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        });

      }

    })
  },
  home() {
    wx.redirectTo({
      url: "/pages/index/index",
    });
  },
});